package cs2340.quarterblack.wheresmystuff;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cs2340.quarterblack.wheresmystuff.Item.Itemtype;

public class ItemPage extends Activity {
	ArrayAdapter<String> adapter;
	
	String userId;
	Item item;
	TextView tvName;
	TextView tvDescription;
	TextView tvLocation;
	TextView tvDate;
	TextView tvCategory;
	TextView tvContact;
	TextView tvOwner;
	Button btnQuickSearch;
	Button btnBack;
	Account account;
	ArrayList<Item> listFound;
	ArrayList<Item> listLost;
	Item curr;
	@Override
	/**
	 * creates the create item page
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupActionBar();
		setContentView(R.layout.activity_item);
		
		tvName = (TextView)findViewById(R.id.ci_itemName);
		tvDescription = (TextView)findViewById(R.id.ci_itemDescription);
		tvLocation = (TextView)findViewById(R.id.ci_itemLocation);
		tvDate = (TextView)findViewById(R.id.ci_itemDate);
		tvCategory = (TextView)findViewById(R.id.ci_itemCategory);
		tvContact = (TextView)findViewById(R.id.ci_contact);
		tvOwner = (TextView)findViewById(R.id.ci_owner);
		item = Model.getItem();
		btnQuickSearch = (Button)findViewById(R.id.btn_quickSearch);
		btnBack = (Button)findViewById(R.id.btn_back);
		
		tvName.setText(item.getName());
		tvDescription.setText(item.getDescription());
		tvLocation.setText(item.getLocation());
		tvDate.setText(item.getDate().toString());
		tvCategory.setText(item.getStringCategory());
		tvContact.setText(item.getContact());
		tvOwner.setText(item.getUserId());
		
		account = Model.getAccount();
		listFound = (ArrayList<Item>)account.getFoundItems();
		listLost = (ArrayList<Item>)account.getLostItems();
		
		btnBack.setOnClickListener(new OnClickListener()
		{
			public void onClick(View arg0) {
				onBackPressed();				
			}
			
		});
		
		btnQuickSearch.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				
				boolean found = false;
				Log.d("wzy", "a");
				if(item.getType() != "found") {
					if(listFound == null) {
						Toast toast = Toast.makeText(getApplicationContext(), "No such item found", 3);
						toast.show();
						return;
					}
				
					for(int i=0;i<listFound.size();i++) {
						curr = listFound.get(i);
					
						if(curr.getName().equals(item.getName()) && curr.getCategory().equals(item.getCategory())) {
							
							found = true;
							Toast toast = Toast.makeText(getApplicationContext(), "Item Found", 3);
							toast.show();
						
							TimerTask task = new TimerTask() {

								@Override
								public void run() {
								// TODO Auto-generated method stub
									Intent itemPage = new Intent(ItemPage.this, ItemPage.class);
									Model.setItem(curr);
									ItemPage.this.startActivity(itemPage);
								}
							
							};
						
							Timer timer = new Timer();
							timer.schedule(task, 3000);
						}
					}
					
					if(!found) {
						Toast.makeText(getApplicationContext(), "No such item found", 3).show();
					}
				} else {
					
					if(listLost == null) {
						Toast toast = Toast.makeText(getApplicationContext(), "No such item Lost", 3);
						toast.show();
						return;
					}
					
					for(int i=0;i<listLost.size();i++) {
						curr = listLost.get(i); 
						
						if(curr.getName().equals(item.getName()) && curr.getCategory().equals(item.getCategory())) {
							found = true;
							Toast toast = Toast.makeText(getApplicationContext(), "Item Found", 3);
							toast.show();
							
							TimerTask task = new TimerTask() {

								@Override
								public void run() {
									// TODO Auto-generated method stub
									Intent itemPage = new Intent(ItemPage.this, ItemPage.class);
									Model.setItem(curr);
									ItemPage.this.startActivity(itemPage);
								}
								
							};
							
							Timer timer = new Timer();
							timer.schedule(task, 3000);
						}
					}
				}
				
				if(!found) {
					Toast.makeText(getApplicationContext(), "No such item lost", 3).show();
				}
			}
		});
	}
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	/**
	 * initializes the menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * decides what to do when an item is selected from the menu
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_logout:
			Intent homepage = new Intent(ItemPage.this,LoginPage.class);
			homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			ItemPage.this.startActivity(homepage);
			return true;
		case R.id.action_settings:
			Intent settings = new Intent(ItemPage.this,LoginPage.class);
			ItemPage.this.startActivity(settings);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
}
